package com.example.shop_2304.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.Car
import com.example.base.mvvm.BaseFragment
import com.example.shop_2304.R
import com.example.shop_2304.adapter.CarAdapter
import com.example.shop_2304.databinding.FragmentCar2Binding


class Car2Fragment : BaseFragment<FragmentCar2Binding,CarViewModel>() {
    lateinit var carAdapter: CarAdapter
    override fun initView() {
       carAdapter = CarAdapter()
        binding.rv.adapter = carAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)
        viewModel.car("true","10","1")
        //点击全选checkbox,上方数据发生变化
        binding.cb.setOnClickListener {
            carAdapter.data.forEach {
                it.isCheck = binding.cb.isChecked
            }
            carAdapter.notifyDataSetChanged()
            checkBottom()//检查底部合计金额
        }
        //点击item上的控件
        carAdapter.addChildClickViewIds(R.id.car_cb,R.id.car_add,R.id.car_sub)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_cb->{
                    carAdapter.data[position].isCheck = (carAdapter.getViewByPosition(position,R.id.car_cb) as CheckBox).isChecked
                }
                R.id.car_add->{
                    carAdapter.data[position].cartNum++
                    (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()
                    viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                }
                R.id.car_sub->{
                    if(carAdapter.data[position].cartNum!=1){
                        carAdapter.data[position].cartNum--
                        (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()
                        viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                    }else{
                        viewModel.deleteCar(arrayOf(carAdapter.data[position].id))
                        //页面进行实时刷新
                        carAdapter.data.removeAt(position)
                        carAdapter.notifyItemRemoved(position)
                    }
                }
            }
            checkBottom()
        }
        //实现批量删除
        binding.tvDelete.setOnClickListener {
            //先判断是否有选中
            var flag = false
            //将数据循环 只有有选中的那么flag设为true
            carAdapter.data.forEach {
                if(it.isCheck){
                    flag = true
                }
            }
            if(flag){
                //进行删除
                //可能会有多个
                var ids = arrayOf<Int>()
                //页面批量删除
                var goods = mutableListOf<Car>()
                carAdapter.data.forEach {
                    if(it.isCheck){
                        ids+=it.id
                        goods.add(it)
                    }
                }
                //循环结束后调用
                viewModel.deleteCar(ids)
                //页面更新
                carAdapter.data.removeAll(goods)
                carAdapter.notifyDataSetChanged()
                checkBottom()
            }else{
                ToastUtils.showLong("请选择商品")
            }
        }
    }

    private fun checkBottom() {
        //计算合计金额  循环数据 检查是选中的 那么数量*单价 累加
        var amount = 0f
        var num = 0
        carAdapter.data.forEach {
            if(it.isCheck){
                //累加  数量*单价
                amount+=it.cartNum * it.price.toFloat()
                num++
            }else{
                //未选中，不执行任何操作
            }
        }
        binding.tvTotal.text = "合计:￥$amount"
        binding.cb.text = "全选($num)"

        //只有点击上方rv中的cb,会影响cb的选中状态
        //当购物车数量为0时，默认cb是不选中的
        if(carAdapter.data.size==0){
            binding.cb.isChecked = false
        }else{
            binding.cb.isChecked =   carAdapter.data.size==num
        }

    }

    override fun initData() {
       viewModel.carSuccess.observe(this){
           if(it.code==200){
               carAdapter.data.clear()
               carAdapter.data.addAll(it.data.list)
               carAdapter.notifyDataSetChanged()
               //显示总数
               binding.tvCount.text = "购物车数量(${carAdapter.data.size})"
           }else{
               ToastUtils.showLong(it.message)
           }
       }
        viewModel.updateSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.deleteSuccess.observe(this){
            ToastUtils.showLong(it.message)

        }
    }

    override fun getLayout(): Int  = R.layout.fragment_car2

    override fun getViewModel(): Class<CarViewModel>  = CarViewModel::class.java

}